package com.mhl.service;

import com.mhl.dao.BillDAO;
import com.mhl.dao.MultiTableDAO;
import com.mhl.domain.Bill;
import com.mhl.domain.MultiTableBean;

import java.util.List;
import java.util.UUID;

/**
 * 处理账单相关的业务
 */
public class BillService {
    // 定义BillDAO属性
    private BillDAO billDAO = new BillDAO();

    // 定义MenuService属性
    private MenuService menuService = new MenuService();

    // 定义DiningTableService属性
    private DiningTableService diningTableService = new DiningTableService();

    private MultiTableDAO multiTableDAO = new MultiTableDAO();

    // 编写点餐的方法
    // 1、生成账单
    // 2、需要更新餐桌的状态
    // 3、成功 返回true 否则返回false
    public boolean orderMenu(int menuId, int nums, int diningTableId) {
        // 生成一个账单编号
        String billId = UUID.randomUUID().toString();

        // 将账单生成到bill表
        int update = billDAO.update("insert into bill values(null, ?, ?, ?, ?, ?, now(), '未结账')",
                billId, menuId, nums, menuService.getMenuById(menuId).getPrice() * nums, diningTableId);

        if (update <= 0) {
            return false;
        }

        // 更新对应餐桌的状态
        return diningTableService.updateDiningTableState(diningTableId, "就餐中");
    }

    // 返回所有账单 提供给View使用
    public List<Bill> list() {
        return billDAO.queryMulti("select * from bill", Bill.class);
    }

    // 返回所有账单并带有菜品名 提供给View使用
    public List<MultiTableBean> list2() {
        return multiTableDAO.queryMulti
                ("SELECT bill.*, `name` FROM bill, menu WHERE bill.menuId=menu.Id", MultiTableBean.class);
    }

    // 查看某个餐桌是否有未结账的账单
    public boolean hasPayBillDiningTableId(int diningTableId) {
        Bill querySingle = billDAO.querySingle
                ("select * from bill where diningTableId=? and state='未结账' limit 0, 1", Bill.class, diningTableId);
        return querySingle != null;
    }

    // 完成结账（如果餐桌存在且该餐桌有未结账的账单）
    public boolean payBill(int diningTableId, String payMode) {
        // 1、修改bill表
        int update = billDAO.update("update bill set state=? where diningTableId=? and state='未结账'",
                payMode, diningTableId);
        if (update < 0) {
            return false;
        }
        // 2、修改diningTable表
        if (!diningTableService.updateDiningTableToFree(diningTableId, "空")) {
            return false;
        }

        return true;
    }
}
